#!/bin/bash
ip_mediator1=10.47.61.114

IP=ip_mediator1
remote_host="jenkins@${!IP}"

AGENT_DIR=/mytijian/agent
HOSPITAL_DIR=${AGENT_DIR}/hospitals
TEMP_DIR=/tmp/agent_tmp
PROBE_TEMP=/tmp/probe_tmp


:>/tmp/alert
:>/tmp/alert_probe
:>/tmp/alert_zip
:>/tmp/alert_git

#切换分支
[ $? != 0 ]&& exit 1
git checkout develop
git pull origin develop

echo "step 1"
#清理临时目录
ssh  ${remote_host} [ -d "$TEMP_DIR" ]

if [ $? = 0 ];then
    ssh  ${remote_host} "rm -rf $TEMP_DIR/*"
else
    ssh  ${remote_host} "mkdir -p ${TEMP_DIR}"
    if [ $? != 0 ];then
        echo "create ${TEMP_DIR} failed">>/tmp/alert
        exit 1
    fi
fi

#解压war包到临时目录
ssh ${remote_host} "mkdir -p $TEMP_DIR/{mediator-agent,control} &&\
    unzip $AGENT_DIR/client/$version/mediator-agent-$version.war -d $TEMP_DIR/mediator-agent &&\
    unzip $AGENT_DIR/client/$version/control-$version.war -d $TEMP_DIR/control"


function delete_proxy_config(){
    if [ -f "cxf-proxy.xml" ];then
        echo "=====删除临时目录${i}代理配置文件====="
        ssh ${remote_host} "rm -f $TEMP_DIR/mediator-agent/WEB-INF/classes/cxf-proxy.xml"
        ssh ${remote_host} "rm -f $TEMP_DIR/control/WEB-INF/classes/cxf-proxy.xml"
        ssh ${remote_host} "rm -f $TEMP_DIR/mediator-agent/WEB-INF/classes/applicationContext.xml"
        ssh ${remote_host} "rm -f $TEMP_DIR/control/WEB-INF/classes/applicationContext.xml"
    fi

}
#遍历医院
for i in $hospital
do

    cd $WORKSPACE/hospital/${i}
    #判断医院编号
    if [ $? = 0 ];then
        echo pass
    else
        echo "[$i] id error<br>">>/tmp/alert
        continue
    fi
    
    echo "=====更新${i}的configwget.sh配置文件====="
    sed -i "s/[0-9]\d*\.\d*[0-9].\d*[0-9]/$version/g" configwget.sh
    echo "${i} alert version">>/tmp/alert_git


    echo "=====创建${i}的agent包目录====="
    ssh ${remote_host} "[ ! -d "$HOSPITAL_DIR/$i" ]&& mkdir -p $HOSPITAL_DIR/$i"


    echo "======拷贝${i}配置文件到临时目录======"
    scp agent-db.properties ${remote_host}:$TEMP_DIR/mediator-agent/WEB-INF/classes/agent-db.properties
    scp config.properties ${remote_host}:$TEMP_DIR/mediator-agent/WEB-INF/classes/config.properties
    #scp tasks.properties ${remote_host}:$TEMP_DIR/mediator-agent/WEB-INF/classes/tasks.properties
    scp control.config.properties ${remote_host}:$TEMP_DIR/control/WEB-INF/classes/config.properties
    scp hisdbip.txt ${remote_host}:$TEMP_DIR/mediator-agent/WEB-INF/classes/hisdbip.txt


    if [ -f "cxf-proxy.xml" ];then
        echo "======由于使用代理,拷贝${i}代理文件到临时目录======"
        scp cxf-proxy.xml ${remote_host}:$TEMP_DIR/mediator-agent/WEB-INF/classes/cxf-proxy.xml
        scp cxf-proxy.xml ${remote_host}:$TEMP_DIR/control/WEB-INF/classes/cxf-proxy.xml
        scp applicationContext.xml ${remote_host}:$TEMP_DIR/mediator-agent/WEB-INF/classes/applicationContext.xml
        scp control-applicationContext.xml ${remote_host}:$TEMP_DIR/control/WEB-INF/classes/applicationContext.xml
    fi

    echo "=====判断${i}的probe是否有值====="
    PROBE=`grep 'mediator_probe' configwget.sh |sed 's/\(.*\)"\(.*\)"\(.*\)/\2/g'`
    if [ -z $PROBE ]; then
        echo "${i} probe error<br>">>/tmp/alert_probe
		#delete_proxy_config
        continue
    else
        ssh ${remote_host} "unzip -o $AGENT_DIR/client/$version/probes/$PROBE -d $TEMP_DIR/mediator-agent/WEB-INF/lib &&\
        cd $TEMP_DIR && zip -r mediator-agent.zip * && mv mediator-agent.zip $HOSPITAL_DIR/$i"
        if [ $? != 0 ]; then
            echo "${i} failed<br>">>/tmp/alert_zip
			#delete_proxy_config
            continue
        else
            echo "${i} sussess<br>">>/tmp/alert
        fi
    fi
    
    echo "=====删除probe中jar包====="
    ssh ${remote_host} "unzip -t $AGENT_DIR/client/$version/probes/$PROBE |grep 'testing'|awk  '{print \$2}' >/tmp/.tmp_probe"
    for jarfile in `ssh ${remote_host} cat /tmp/.tmp_probe`
    do
        ssh ${remote_host} "find $TEMP_DIR/mediator-agent/WEB-INF/lib -type f -name "$jarfile" -exec rm -f {} \;"
    done

    if [ -f "cxf-proxy.xml" ];then
        echo "=====删除临时目录${i}代理配置文件====="
        ssh ${remote_host} "rm -f $TEMP_DIR/mediator-agent/WEB-INF/classes/cxf-proxy.xml"
        ssh ${remote_host} "rm -f $TEMP_DIR/control/WEB-INF/classes/cxf-proxy.xml"
        ssh ${remote_host} "rm -f $TEMP_DIR/mediator-agent/WEB-INF/classes/applicationContext.xml"
        ssh ${remote_host} "rm -f $TEMP_DIR/control/WEB-INF/classes/applicationContext.xml"
    fi
done

echo "=====提交代码===="
git config --global user.email "cwshi@mytijian.com"
git config --global user.name "cwshi"

cd $WORKSPACE
git add *
str_commit=`cat /tmp/alert_git`
git commit -m "$str_commit"
git push origin develop

echo "======end======"